package dxn.nl.file.api.export;

import com.alibaba.fastjson2.JSONObject;
import dxn.nl.file.client.core.CsExportDealServer;
import dxn.nl.file.client.core.DealExportService;
import dxn.nl.file.client.core.SteamQueryCallBackService;
import dxn.nl.file.client.model.dto.*;
import dxn.nl.file.client.model.enums.ExportQueryDataTypeEnum;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@CsExportDealServer(value = {"demo_export_stream_muti_sheet"})
public class DealStreamMutilSheetExport implements DealExportService {
    @Override
    public CsExportDealTypeHeadDto getExcelHeads(TransferMessageConfig config, TransferMessageData messageData, byte[] data) {
        List<CsExportExcelHeadDto> dtoList = Arrays.asList(new CsExportExcelHeadDto("code","编号",1,30),
                new CsExportExcelHeadDto("name","名称",2,15),
                new CsExportExcelHeadDto("age","年龄",3,10),
                new CsExportExcelHeadDto("address","地址",20));
        CsExportDealTypeHeadDto dto = new CsExportDealTypeHeadDto();
        dto.setExportType(ExportQueryDataTypeEnum.stream);
        dto.setDefaultSheetHeads(dtoList);

        List<CsExportExcelHeadDto> sheet2List = Arrays.asList(new CsExportExcelHeadDto("code","sheet2编号",1,30),
                new CsExportExcelHeadDto("name","sheet2名称",2,15),
                new CsExportExcelHeadDto("age","sheet2年龄",3,10),
                new CsExportExcelHeadDto("address","sheet2地址",20));

        CsExportExcelSheetHeadDto sheetHeadDto = new CsExportExcelSheetHeadDto();
        sheetHeadDto.setHeadDtoList(sheet2List);
        sheetHeadDto.setHasTitle(true);

        dto.setSheetHeads(sheetHeadDto,"测试sheet2");
        return dto;
    }

    @Override
    public String streamQueryData(TransferMessageConfig config, TransferMessageData messageData, byte[] data, SteamQueryCallBackService steamQueryCallBackService){
        List<JSONObject> list = new ArrayList<>();
        for(int i=0;i<10;i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "code"+i);
            jsonObject.put("name", "name"+i);
            jsonObject.put("age", "age"+i);
            jsonObject.put("address", "address"+i);
            list.add(jsonObject);
        }

        // 写入默认表格
        steamQueryCallBackService.handleResult(CsExportDealTypeHeadDto.defaultSheetName,list);
        // 写入第二个自定义的表格
        steamQueryCallBackService.handleResult("测试sheet2",list);
        return null;
    }
}
